package com.woniuxy.joylifeshop.service.serviceImpl;

import com.woniuxy.common.entity.ShopMerchant;
import com.woniuxy.common.entity.ShopMerchantAccount;
import com.woniuxy.common.entity.ShopMerchantAccountExample;
import com.woniuxy.common.entity.ShopOrderItem;
import com.woniuxy.common.mapper.ShopMerchantAccountMapper;
import com.woniuxy.joylifeshop.exception.MerchantAccountException;
import com.woniuxy.joylifeshop.service.MerchantAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MerchantAccountServiceImpl implements MerchantAccountService {

    @Autowired
    ShopMerchantAccountMapper shopMerchantAccountMapper;
    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public void modifyByPurchase(ShopOrderItem shopOrderItem) {
        try {
            //查出原对象
            ShopMerchantAccountExample shopMerchantAccountExample = new ShopMerchantAccountExample();
            ShopMerchantAccountExample.Criteria criteria = shopMerchantAccountExample.createCriteria();
            criteria.andMerchantIdEqualTo(shopOrderItem.getMerchantId());

            List<ShopMerchantAccount> shopMerchantAccounts = shopMerchantAccountMapper.selectByExample(shopMerchantAccountExample);
            //修改
            shopMerchantAccounts.get(0).setBalance(shopMerchantAccounts.get(0).getBalance()+shopOrderItem.getPrice());
            shopMerchantAccounts.get(0).setTurnover(shopMerchantAccounts.get(0).getTurnover()+shopOrderItem.getPrice());
            //再存入
            shopMerchantAccountMapper.updateByPrimaryKey(shopMerchantAccounts.get(0));
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantAccountException();
        }

    }
    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public List<ShopMerchantAccount> readMerchantAccountObject(ShopMerchant shopMerchant) {


        List<ShopMerchantAccount> shopMerchantAccounts = null;
        try {
            ShopMerchantAccountExample shopMerchantAccountExample = new ShopMerchantAccountExample();
            ShopMerchantAccountExample.Criteria criteria = shopMerchantAccountExample.createCriteria();
            criteria.andMerchantIdEqualTo(shopMerchant.getMerchantId());

            shopMerchantAccounts = shopMerchantAccountMapper.selectByExample(shopMerchantAccountExample);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantAccountException();
        }


        return shopMerchantAccounts;
    }

    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public void modifyByWithdraw(ShopMerchantAccount shopMerchantAccount) {


        try {
            //找出原对象
            ShopMerchantAccount shopMerchantAccountReality = shopMerchantAccountMapper.selectByPrimaryKey(shopMerchantAccount.getAccountId());

            //修改原对象数据
            shopMerchantAccountReality.setBalance(shopMerchantAccountReality.getBalance()-shopMerchantAccount.getBalance());

            //存入
            shopMerchantAccountMapper.updateByPrimaryKey(shopMerchantAccountReality);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantAccountException();
        }


    }

    @Override
    public ShopMerchantAccount readMerchantAccountByPrimaryId(ShopMerchantAccount shopMerchantAccount) {


        try {
            ShopMerchantAccount shopMerchantAccount1 = shopMerchantAccountMapper.selectByPrimaryKey(shopMerchantAccount.getAccountId());

            return shopMerchantAccount1;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantAccountException();
        }
    }
}
